from datetime import datetime, date, timedelta

from django.db import transaction
from django.db.models import Q
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework import serializers
from rest_framework.generics import ListCreateAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import GoodsVisitCount, SPUSpecification, SPU, SKUSpecification
from apps.meiduo_admin.serializers.skus_serializers import SkUSerializer, SKUCategorieSerializer, SPUSpecSerialzier

from apps.meiduo_admin.utils.pagenum import PageNum
from apps.orders.models import OrderInfo
from apps.statistical.serializers import GoodSerializer
from apps.users.models import User
from django.utils import timezone
from rest_framework import viewsets
from apps.goods.models import SKU, GoodsCategory
from celery_tasks.detail.tasks import get_detail_html


# 获取商品信息
class SKUGoodsView(ModelViewSet):
    # 指定序列化器
    serializer_class = SkUSerializer
    # 指定分页器
    pagination_class = PageNum
    # 通过方法指定查询集
    def get_queryset(self):
        # 用get取字典值时  不存在返回None   用[""]取值时会报错
        keyword = self.request.query_params.get("keyword")
        queryset = SKU.objects
        if keyword:
            queryset = queryset.filter(Q(name__contains=keyword) | Q(caption__contains=keyword))
        queryset = queryset.order_by("-id")
        return queryset
# 获取商品的三级分类
class SKUCategorieView(ListAPIView):
    serializer_class = SKUCategorieSerializer
    # 没有子集则为第三级分类  自关联有两个属性 parent为父属性  subs为子属性
    queryset = GoodsCategory.objects.filter(subs__isnull=True)


# 获取商品的spu
class SPUSpecView(ListAPIView):
    serializer_class = SPUSpecSerialzier
    def get_queryset(self):
        # 获取spu_id
        # self.kwargs接受路径中的关键字参数   self.args  接受位置参数
        spu_id = self.kwargs.get("pk")
        return SPUSpecification.objects.filter(spu_id=spu_id)

